package com.dxx.cloud.admin.controller.admin;

import com.dxx.cloud.admin.module.entity.CustomerEntity;
import com.dxx.cloud.admin.module.entity.UserEntity;
import com.dxx.cloud.admin.module.request.SendMsgRequest;
import com.dxx.cloud.admin.module.request.UserLoginRequest;
import com.dxx.cloud.admin.service.ICustomerService;
import com.dxx.cloud.admin.service.IUserService;
import com.dxx.cloud.admin.service.sms.ISmsService;
import com.dxx.cloud.admin.utils.Md5Utils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.dromara.common.core.domain.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/user")
@Tag(name = "登录模块")
public class LoginController {

    @Resource
    private IUserService userService;

    @Resource
    private ICustomerService customerService;

    @Resource
    private ISmsService smsService;

    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录")
    public R<UserEntity> login(@RequestBody @Validated UserLoginRequest request) {
        request.setPassword(Md5Utils.encode(request.getPassword()));
        return R.ok(userService.login(request.getMobile(),request.getPassword()));
    }

    @PostMapping("/loginByCode")
    @Operation(summary = "验证码登录", description = "登录")
    public R<CustomerEntity> loginByCode(@RequestBody @Validated UserLoginRequest request) {
        return R.ok(customerService.loginByCode(request.getMobile(),request.getCode()));
    }

    @PostMapping("/sendVerifyCode")
    @Operation(summary = "发送验证短信", description = "发送验证短信")
    public R<Boolean> login(@RequestBody @Validated SendMsgRequest request) {
        final boolean result = userService.sendVerificationCode(request.getMobile());
        return R.ok(result);
    }


}
